// pages/orderDetail/orderDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total:0,
    peijian:[],
    order_mun:'',


    list: {},
    showModal: false,
    select: '',//选择的优惠券id
    selectprice:0,//选择的优惠券金额
    isshow:false,

    dianzan:0,
    dianzan_status: false,


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      order_mun: options.order_mun,
      
    })
    wx.request({
      url: 'https://wukongweixiu.cn/index/Defaults/getmycoupon',
      data: {
        userid: getApp().globalData.id,
        order_mun: options.order_mun
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        that.setData({
          list: res.data.data.coupon,
          isshow: res.data.data.showModal,
          dianzan: res.data.data.dianzan
        })
       
      }
    })
  },
  // 外面的弹窗
  selcet: function (e) {
    var page = this;
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.key;
    var totalPrice = page.data.total;
    var dianzan = page.data.dianzan;
    if (totalPrice < price) {
      return false;
    } else {
      // totalPrice = (totalPrice - price) <= 0 ? 0 : totalPrice - price;
      dianzan=false;
    }

    var list = page.data.list;
    for (var key in list) {
      if (key == id) {
        list[key].is_select = true
        var id = list[key].id;
      } else {
        list[key].is_select = false;
      }
    }
   
    page.setData({
      list: list,
      select: id,
      selectprice: price,
      dianzan: dianzan,
    });
  },
  coupon: function () {
    var page = this;
    page.setData({
      showModal: true,
      dianzan_status:false,
    })
  },
  quedin: function () {
    var that = this;
    var total = that.data.total;
    var selectprice = that.data.selectprice;
    console.log(total - selectprice)
    that.setData({
      showModal: false
    })
  },
  close:function(){
    var that = this;
    this.setData({
      selectprice: 0,
      select:'',
      showModal: false
    })
  },
  dianzan:function(e){
    var page = this;
    var dianzan_mun = page.data.dianzan;
    var total = page.data.total;
    var dianzan_status = page.data.dianzan_status;
    var isshow = page.data.isshow;
    var total_mun = 0;
    console.log(dianzan_status)
    if (dianzan_status == true){
      dianzan_status = false;
      total_mun = total + dianzan_mun;
    }else{
      dianzan_status = true;
      total_mun = total - dianzan_mun;
    }
    if (isshow == true){
      isshow = false;
    }else{
      isshow = true;
    }

    page.setData({
      total: total_mun,
      isshow: isshow,
      dianzan_status: dianzan_status
    })
  },
  addCard: function () {
    var that =this
    if (that.data.total <=0){
      return false;
    }
    //修改订单的优惠券或点赞优惠金额
    // wx.request({
    //   url: 'https://wukongweixiu.cn/index/Order/getweixiouh',
    //   data: {
    //     order_mun: that.data.order_mun,
    //     select: that.data.selcet,//选择的优惠券id
    //     selectprice: that.data.selectprice,//选择的优惠券金额
    //     dianzan: that.data.dianzan,
    //   },
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   method: 'GET',
    //   success: function (res) {

        
    //   }
    // })

    //跳去支付
    var data={
      order_mun: that.data.order_mun,
      totalPrice: that.data.total,
      type:'weikuan',
      couponid: that.data.select,
      dianzan_status: that.data.dianzan
    }
    wx.navigateTo({
      url: '../cashierDesk/cashierDesk?data=' + JSON.stringify(data),
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that =this
    wx.request({
      url: 'https://wukongweixiu.cn/index/Order/getweixiouh',
      data: {
        order_mun: that.data.order_mun,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
   
        that.setData({
          peijian: res.data.data,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})